package com.youlai.system.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.model.bo.AssetComputerBO;
import com.youlai.system.model.entity.AssetComputer;
import com.youlai.system.model.form.ComputerForm;
import com.youlai.system.model.vo.AssetComputerPageVO;
import com.youlai.system.security.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class AssetComputerConverter {

    public Page<AssetComputerPageVO> bo2PageVo(Page<AssetComputerBO> bo) {
        if ( bo == null ) {
            return null;
        }

        Page<AssetComputerPageVO> page = new Page<AssetComputerPageVO>();

        page.setPages( bo.getPages() );
        page.setRecords( computerBOListToComputerPageVOList( bo.getRecords() ) );
        page.setTotal( bo.getTotal() );
        page.setSize( bo.getSize() );
        page.setCurrent( bo.getCurrent() );

        return page;
    }

    private List<AssetComputerPageVO> computerBOListToComputerPageVOList(List<AssetComputerBO> list) {
        if ( list == null ) {
            return null;
        }

        List<AssetComputerPageVO> list1 = new ArrayList<>( list.size() );
        for ( AssetComputerBO assetComputerBO : list ) {
            AssetComputerPageVO assetComputerPageVO = new AssetComputerPageVO();
            BeanUtils.copyProperties(assetComputerBO, assetComputerPageVO);
            list1.add(assetComputerPageVO);
        }

        return list1;
    }

    public AssetComputer form2entity(ComputerForm computerForm) {
        AssetComputer assetComputer = new AssetComputer();
        BeanUtils.copyProperties(computerForm, assetComputer);
        assetComputer.setCreateBy(Objects.requireNonNull(SecurityUtils.getUser()).getUserId());
        return assetComputer;
    }
}
